#bilibili排名算法 v2.0(2010.9)
#点击分 = 点击数 * (2/(P+1))
#评论分 = 评论数 * 50*(点击分/（点击分+评论数*50）)
#收藏分 = 收藏数 * 100
#总分 = 点击分 + 评论分 + 收藏分
class Results::R20 < Results::Base
    #评论倍值
    E_C = 50
   #收藏倍值
    E_S = 100
  
  @space_fix = nil

  def get_ranking_point
    clicks_point = clicks * click_revised
    comment_point = comments * comment_revised
    format("%.2f", (clicks_point + comment_point + stows * E_S) * space_fix).to_f
  end

  #娱乐版&youku|tudou修正值
  def space_fix
    if @space_fix
      @space_fix
    else
      if @@space_fix_cache
          if @@space_fix_cache[work.wid]
            @space_fix = @@space_fix_cache[work.wid]
          else
             if ([19, 20, 21, 22, 25, 26].include? work.ac_type_id) && !work.youku_and_tudou.empty?
               @space_fix = (@@space_fix_cache[work.wid] = 0.5)
             else
               @space_fix = (@@space_fix_cache[work.wid] = 1)
             end
          end
      else
        if ([19, 20, 21, 22, 25, 26].include? work.ac_type_id) && !work.youku_and_tudou.empty?
          @space_fix = 0.5
        else
          @space_fix = 1
        end
      end
    end
  end
  
  #点击Part修正
  def self.get_click_revised parts, simple = true
    simple ? format("%.2f", 2.0 / (parts + 1)).to_f : 1.0 / parts
  end

  def click_revised simple = true
    self.class.get_click_revised work.part_count, simple
  end
  
  #评论修正
  def self.get_comment_revised fixed_clicks, comments
    if comments <= 0
      0.0
    else
      format("%.2f", E_C * fixed_clicks.to_f / (fixed_clicks.to_f + comments.to_f * E_C)).to_f
    end
  end

  def comment_revised
    self.class.get_comment_revised clicks * click_revised, comments
  end
  
  def self.point_str point
    point_s = point.to_s
    fushu = point_s.first == '-' ? true : false
    size = point_s.size - ( fushu ? 1 : 0 )
    if size <= 3
      point_s
    else
      str = ""
      point_s.delete('-').split('').each_with_index do |ch, i|
        str << "," if i > 0 && (size - i) % 3 == 0
        str << ch
      end
      fushu ? '-' + str : str
    end
  end
end
